//
//  CShareMgr.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/3/1.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

//EFENGSHARE(数据库名称)
#import "CShareMgr.h"
#import "EFengShare.h"
#import "FMDB.h"
#import "NSArray+Extension.h"

@implementation CShareMgr

- (BOOL)insertWithShare:(EFengShare *)share
{
    if(share == nil)
    {
        return NO;
    } else {
        
    }
    
    __block BOOL ret = NO;
    
    NSString *sql = nil;
    sql = [NSString stringWithFormat:@"select * from EFENGSHARE where shareId = \'%@\'", share.shareId];
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        FMResultSet *result = [db executeQuery:sql];
        if (result && [result next]) {
            ret = YES;
        }
        [result close];
        if (ret) {
            ret = [db executeUpdate:@"UPDATE EFENGSHARE SET videoName = ?, imageName = ?, TimeStamp = ? where shareId = ?", M_STOREVALUE([share.videoName jsonString]),M_STOREVALUE([share.imageName jsonString]), M_STOREVALUE(share.date),M_STOREVALUE(share.shareId)];
        } else {
            ret = [db executeUpdate:@"INSERT INTO EFENGSHARE(shareId, releaseUserName, releaseUserId, visibleUserId, praiseUserId, praiseCount, releaseType, imageName, content, companyId, TimeStamp, isRead, videoName,isClick,isLocal,isShowShanchu,isZan,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",M_STOREVALUE(share.shareId), M_STOREVALUE(share.releaseUserName), M_STOREVALUE(share.releaseUserId),M_STOREVALUE(share.visibleUserId), M_STOREVALUE(share.praiseUserId), M_STOREVALUE(share.praiseCount), M_STOREVALUE(share.releaseType), M_STOREVALUE([share.imageName jsonString]), M_STOREVALUE(share.content), M_STOREVALUE(share.companyId), M_STOREVALUE(share.date),share.isRead?@1:@0,M_STOREVALUE([share.videoName jsonString]),share.isClick?@1:@0,share.isLocal?@1:@0,share.isShowShanchu?@1:@0,share.isZan?@1:@0,M_STOREVALUE(share.yuLiuZiduan1),@(share.yuLiuZiduan2),M_STOREVALUE(share.yuLiuZiduan3)];
        }
        
    }];
    return ret;
    
}
-(NSArray *)getAllUnsendShare
{
    __block NSMutableArray *arrayM = [NSMutableArray array];
    NSString *sql = @"select * from EFENGSHARE where yuLiuZiduan2 = 1";
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
         FMResultSet *result = [db executeQuery:sql];
        while (result && [result next]) {
            EFengShare *share = [[EFengShare alloc] init];
            share.ShareIndex = [result intForColumn:@"ShareIndex"];
            share.shareId = [result stringForColumn:@"shareId"];
            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
            share.praiseCount = [result stringForColumn:@"praiseCount"];
            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
            share.releaseType = [result stringForColumn:@"releaseType"];
            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
            share.videoName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"videoName"]];
            share.content = [result stringForColumn:@"content"];
            share.companyId = [result stringForColumn:@"companyId"];
            share.date = [result stringForColumn:@"TimeStamp"];
            share.isRead = [result boolForColumn:@"isRead"];
            share.isClick = [result boolForColumn:@"isClick"];
            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
            share.isZan = [result boolForColumn:@"isZan"];
            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [arrayM addObject:share];
            share = nil;
        }
        [result close];
    }];
    return [arrayM copy];
}
- (BOOL)batchInsertWithShareList:(NSArray *)shareList
{
    if(shareList == nil && [shareList count] <= 0)
    {
        return NO;
    }
    //     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(EFengShare *share in shareList)
        {
            ret = NO;
            FMResultSet *result = [db executeQuery:@"select * from EFENGSHARE where  shareId = ?", share.shareId];
            if (result && [result next]) {
                ret = YES;
            }
            [result close];

            if (ret) {
                 ret = [db executeUpdate:@"UPDATE EFENGSHARE SET videoName = ?,imageName = ?, TimeStamp = ? where shareId = ?",M_STOREVALUE([share.videoName jsonString]), M_STOREVALUE([share.imageName jsonString]), M_STOREVALUE(share.date),M_STOREVALUE(share.shareId)];
            } else {
                if (!M_ISSTRNULL(share.shareId)) {
                    ret = [db executeUpdate:@"INSERT INTO EFENGSHARE(shareId, releaseUserName, releaseUserId, visibleUserId, praiseUserId, praiseCount, releaseType, imageName, videoName, content, companyId, TimeStamp, isRead, isClick,isLocal,isShowShanchu,isZan,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",M_STOREVALUE(share.shareId), M_STOREVALUE(share.releaseUserName), M_STOREVALUE(share.releaseUserId),M_STOREVALUE(share.visibleUserId), M_STOREVALUE(share.praiseUserId), M_STOREVALUE(share.praiseCount), M_STOREVALUE(share.releaseType), M_STOREVALUE([share.imageName jsonString]), M_STOREVALUE([share.videoName jsonString]),M_STOREVALUE(share.content), M_STOREVALUE(share.companyId), M_STOREVALUE(share.date),share.isRead?@1:@0,share.isClick?@1:@0,share.isLocal?@1:@0,share.isShowShanchu?@1:@0,share.isZan?@1:@0,M_STOREVALUE(share.yuLiuZiduan1),@(share.yuLiuZiduan2),M_STOREVALUE(share.yuLiuZiduan3)];
                    if(!ret)
                    {
                        *rollback = YES;
                    }
                    
                }

            }
            
        }
        
    }];
    return ret;

}
- (BOOL)updateWithShare:(EFengShare *)share
{
    if (share == nil) {
        return NO;
    }
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE EFENGSHARE SET releaseUserName = ?, releaseUserId = ?, visibleUserId = ?, praiseUserId = ?, praiseCount = ?, releaseType = ?, imageName = ?, videoName = ?, content = ?, companyId = ?, TimeStamp = ?, isRead = ?, isClick = ? ,yuLiuZiduan1 = ?,yuLiuZiduan2 = ?,yuLiuZiduan3 = ? where shareId = ?", M_STOREVALUE(share.releaseUserName), M_STOREVALUE(share.releaseUserId),M_STOREVALUE(share.visibleUserId), M_STOREVALUE(share.praiseUserId), M_STOREVALUE(share.praiseCount), M_STOREVALUE(share.releaseType), M_STOREVALUE([share.imageName jsonString]), M_STOREVALUE([share.videoName jsonString]), M_STOREVALUE(share.content), M_STOREVALUE(share.companyId), M_STOREVALUE(share.date),share.isRead?@1:@0,share.isClick?@1:@0,M_STOREVALUE(share.yuLiuZiduan1),@(share.yuLiuZiduan2),M_STOREVALUE(share.yuLiuZiduan3),M_STOREVALUE(share.shareId)];

    }];
    return ret;
}
- (BOOL)updateZanWithShareId:(NSString *)shareId isZan:(BOOL)isZan
{
    if (M_ISSTRNULL(shareId)) {
        return NO;
    }
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"UPDATE EFENGSHARE SET isZan = ? where shareId = ?",isZan?@1:@0,M_STOREVALUE(shareId)];
        
    }];
    return ret;

}
- (BOOL)batchUpdateWithShareList:(NSArray *)shareList
{
    if(shareList == nil && [shareList count] <= 0)
    {
        return NO;
    }
    //     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(EFengShare *share in shareList)
        {
            
            ret = [db executeUpdate:@"UPDATE EFENGSHARE SET releaseUserName = ?, releaseUserId = ? , visibleUserId = ?, praiseUserId = ?, praiseCount = ?, releaseType = ?, imageName = ?,videoName = ?,  content = ?, companyId = ?, TimeStamp = ?, isRead = ?, isClick = ?, yuLiuZiduan1 = ?,yuLiuZiduan2 = ?,yuLiuZiduan3 = ? where shareId = ?", M_STOREVALUE(share.releaseUserName), M_STOREVALUE(share.releaseUserId),M_STOREVALUE(share.visibleUserId),M_STOREVALUE(share.praiseUserId), M_STOREVALUE(share.praiseCount), M_STOREVALUE(share.releaseType), M_STOREVALUE([share.imageName jsonString]), M_STOREVALUE([share.videoName jsonString]), M_STOREVALUE(share.content), M_STOREVALUE(share.companyId), M_STOREVALUE(share.date),share.isRead?@1:@0,share.isClick?@1:@0,M_STOREVALUE(share.yuLiuZiduan1),@(share.yuLiuZiduan2),M_STOREVALUE(share.yuLiuZiduan3),M_STOREVALUE(share.shareId)];
                if(!ret)
                {
                    *rollback = YES;
                }
                
        }
        
    }];
    return ret;

}
- (NSInteger)getShareAllUnReadCount
{
    __block NSInteger count = 0;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
       
        count = [db intForQuery:@"select COUNT(*) from EFENGSHARE where isRead = 0 AND isLocal = 0 AND isDel = 0"];
    }];
    return count;
}

- (NSInteger)getAttentionShareAllUnReadCount
{
    __block NSInteger count = 0;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        count = [db intForQuery:@"select COUNT(*) from EFENGSHARE where releaseUserId in (select UserId from ATTENTIONCONTACT where isAttentionMe = 0) AND isRead = 0 AND isLocal = 0 AND isDel = 0"];
    }];
    return count;
}
- (BOOL)updateAllUnreadShareToReadShare
{
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"UPDATE EFENGSHARE SET isRead = 1 where isRead = 0"];
    }];
    return ret;
}

- (BOOL)batchUpdateAllUnreadShareToReadShareWithShareList:(NSArray *)shareList
{
    if(shareList == nil && [shareList count] <= 0)
    {
        return NO;
    }
    //     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(EFengShare *share in shareList)
        {
            ret = [db executeUpdate:@"UPDATE EFENGSHARE SET isRead = 1 where shareId = ?",M_STOREVALUE(share.shareId)];
            if(!ret)
            {
                *rollback = YES;
            }
            
        }
        
    }];
    return ret;
}
//加载最新的20条分享
- (NSArray *)getNewerShareWithMaxTimeStamp:(NSString *)timeStamp
{
    __block NSMutableArray *arrayM = [NSMutableArray array];
    __block NSString *sql = nil;
    if (!M_ISSTRNULL(timeStamp)) {
        sql = [NSString stringWithFormat:@"select * from EFENGSHARE where isLocal = 0 AND isDel = 0 AND TimeStamp > \'%@\' order by TimeStamp  DESC",timeStamp];
    } else {
        sql = [NSString stringWithFormat:@"select * from EFENGSHARE where isLocal = 0 AND isDel = 0 order by TimeStamp  DESC LIMIT \'%@\',\'%@\'", @"0",@"20"];
    }
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while (result && [result next]) {
            EFengShare *share = [[EFengShare alloc] init];
            share.ShareIndex = [result intForColumn:@"ShareIndex"];
            share.shareId = [result stringForColumn:@"shareId"];
            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
            share.praiseCount = [result stringForColumn:@"praiseCount"];
            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
            share.releaseType = [result stringForColumn:@"releaseType"];
            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
            share.videoName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"videoName"]];
            share.content = [result stringForColumn:@"content"];
            share.companyId = [result stringForColumn:@"companyId"];
            share.date = [result stringForColumn:@"TimeStamp"];
            share.isRead = [result boolForColumn:@"isRead"];
            share.isClick = [result boolForColumn:@"isClick"];
            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
            share.isZan = [result boolForColumn:@"isZan"];
            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [arrayM addObject:share];
            share = nil;
        }
        [result close];
    }];
    return arrayM;
}
//加载之前的20条分享
- (NSArray *)getOlderShareWithMinTimeStamp:(NSString *)timeStamp
{
    __block NSMutableArray *arrayM = [NSMutableArray array];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARE where isLocal = 0 AND isDel = 0 AND TimeStamp < ? order by TimeStamp  DESC",timeStamp];
        while ([result next]) {
            EFengShare *share = [[EFengShare alloc] init];
            share.ShareIndex = [result intForColumn:@"ShareIndex"];
            share.shareId = [result stringForColumn:@"shareId"];
            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
            share.praiseCount = [result stringForColumn:@"praiseCount"];
            share.releaseType = [result stringForColumn:@"releaseType"];
            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
            share.videoName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"videoName"]];
            share.content = [result stringForColumn:@"content"];
            share.companyId = [result stringForColumn:@"companyId"];
            share.date = [result stringForColumn:@"TimeStamp"];
            share.isRead = [result boolForColumn:@"isRead"];
            share.isClick = [result boolForColumn:@"isClick"];
            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
            share.isZan = [result boolForColumn:@"isZan"];
            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [arrayM addObject:share];
            share = nil;
        }
        [result close];
    }];
    return arrayM;

}

- (NSArray *)getAttentionNewerShareWithMaxTimeStamp:(NSString *)timeStap curUserId:(NSString *)curUserId
{
    __block NSMutableArray *arrayM = [NSMutableArray array];
    __block NSString *sql = nil;
    if (!M_ISSTRNULL(timeStap)) {
        sql = [NSString stringWithFormat:@"select * from EFENGSHARE where isLocal = 0 AND isDel = 0 AND TimeStamp > \'%@\' AND (releaseUserId = \'%@\' OR releaseUserId in (select UserId from ATTENTIONCONTACT where isAttentionMe = 0)) order by TimeStamp  DESC",timeStap,curUserId];
    } else {
        sql = [NSString stringWithFormat:@"select * from EFENGSHARE where isLocal = 0 AND isDel = 0 AND (releaseUserId = \'%@\' OR releaseUserId in (select UserId from ATTENTIONCONTACT where isAttentionMe = 0)) order by TimeStamp DESC LIMIT \'%@\',\'%@\'", curUserId,@"0",@"20"];
    }

    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            EFengShare *share = [[EFengShare alloc] init];
            share.ShareIndex = [result intForColumn:@"ShareIndex"];
            share.shareId = [result stringForColumn:@"shareId"];
            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
            share.praiseCount = [result stringForColumn:@"praiseCount"];
            share.releaseType = [result stringForColumn:@"releaseType"];
            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
            share.videoName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"videoName"]];
            share.content = [result stringForColumn:@"content"];
            share.companyId = [result stringForColumn:@"companyId"];
            share.date = [result stringForColumn:@"TimeStamp"];
            share.isRead = [result boolForColumn:@"isRead"];
            share.isClick = [result boolForColumn:@"isClick"];
            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
            share.isZan = [result boolForColumn:@"isZan"];
            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [arrayM addObject:share];
            share = nil;
        }
        [result close];
    }];
    return arrayM;

}

- (NSArray *)getAttentionOlderShareWithMinTimeStamp:(NSString *)timeStamp curUserId:(NSString *)curUserId
{
    __block NSMutableArray *arrayM = [NSMutableArray array];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARE where (releaseUserId = ? OR releaseUserId in (select UserId from ATTENTIONCONTACT where isAttentionMe = 0)) AND isLocal = 0 AND isDel = 0 AND TimeStamp < ? order by TimeStamp  DESC",curUserId,timeStamp];
        while (result && [result next]) {
            EFengShare *share = [[EFengShare alloc] init];
            share.ShareIndex = [result intForColumn:@"ShareIndex"];
            share.shareId = [result stringForColumn:@"shareId"];
            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
            share.praiseCount = [result stringForColumn:@"praiseCount"];
            share.releaseType = [result stringForColumn:@"releaseType"];
            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
            share.videoName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"videoName"]];
            share.content = [result stringForColumn:@"content"];
            share.companyId = [result stringForColumn:@"companyId"];
            share.date = [result stringForColumn:@"TimeStamp"];
            share.isRead = [result boolForColumn:@"isRead"];
            share.isClick = [result boolForColumn:@"isClick"];
            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
            share.isZan = [result boolForColumn:@"isZan"];
            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [arrayM addObject:share];
            share = nil;
        }
        [result close];
    }];
    return arrayM;

}


//- (NSArray *)getAttentionNewestShareDataWithCurUserId:(NSString *)curUserId
//{
//    __block NSMutableArray *arrayM = [NSMutableArray array];
//    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
//        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARE where isLocal = 0 AND isDel = 0 AND (releaseUserId = ? OR releaseUserId in (select UserId from ATTENTIONCONTACT where isAttentionMe = 0)) order by TimeStamp DESC LIMIT ?,?", curUserId,@"0",@"20"];
//        while (result && [result next]) {
//            EFengShare *share = [[EFengShare alloc] init];
//            share.ShareIndex = [result intForColumn:@"ShareIndex"];
//            share.shareId = [result stringForColumn:@"shareId"];
//            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
//            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
//            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
//            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
//            share.praiseCount = [result stringForColumn:@"praiseCount"];
//            share.releaseType = [result stringForColumn:@"releaseType"];
//            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
//            share.content = [result stringForColumn:@"content"];
//            share.companyId = [result stringForColumn:@"companyId"];
//            share.date = [result stringForColumn:@"TimeStamp"];
//            share.isRead = [result boolForColumn:@"isRead"];
//            share.isClick = [result boolForColumn:@"isClick"];
//            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
//            share.isZan = [result boolForColumn:@"isZan"];
//            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
//            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
//            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
//            [arrayM addObject:share];
//            share = nil;
//        }
//    }];
//    return arrayM;
//}

- (BOOL)delShareWithShareId:(NSString *)shareId
{
    if (M_ISSTRNULL(shareId)) {
        return NO;
    }
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
       ret = [db executeUpdate:@"delete from EFENGSHARE where shareId = ?",shareId];
    }];
    return ret;
}

//- (NSArray *)getNewestShareData
//{
//    __block NSMutableArray *arrayM = [NSMutableArray array];
//    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
//       FMResultSet *result = [db executeQuery:@"select * from EFENGSHARE where isLocal = 0 AND isDel = 0 order by TimeStamp  DESC LIMIT ?,?", @"0",@"20"];
//        while (result && [result next]) {
//            EFengShare *share = [[EFengShare alloc] init];
//            share.ShareIndex = [result intForColumn:@"ShareIndex"];
//            share.shareId = [result stringForColumn:@"shareId"];
//            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
//            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
//            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
//            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
//            share.praiseCount = [result stringForColumn:@"praiseCount"];
//            share.releaseType = [result stringForColumn:@"releaseType"];
//            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
//            share.content = [result stringForColumn:@"content"];
//            share.companyId = [result stringForColumn:@"companyId"];
//            share.date = [result stringForColumn:@"TimeStamp"];
//            share.isRead = [result boolForColumn:@"isRead"];
//            share.isClick = [result boolForColumn:@"isClick"];
//            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
//            share.isZan = [result boolForColumn:@"isZan"];
//            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
//            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
//            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
//            [arrayM addObject:share];
//            share = nil;
//        }
//    }];
//    return arrayM;
//}

- (NSString *)getMinDateShare
{
    __block NSString *result = nil;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        result = [db stringForQuery:@"select min(TimeStamp) from EFENGSHARE"];
    }];
    return result;
}


- (NSString *)getMaxDateShare
{
    __block NSString *result = nil;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        result = [db stringForQuery:@"select max(TimeStamp) from EFENGSHARE"];
    }];
    return result;
}

- (NSString *)getShareContentWithShareId:(NSString *)shareId
{
    __block NSString *result = nil;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        result = [db stringForQuery:@"select content from EFENGSHARE where shareId = ?",shareId];
    }];
    return result;
}

- (NSArray *)getShareImageNameWithShareId:(NSString *)shareId
{
    __block NSArray *result = nil;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        result = [NSArray arrayWihtJsonStr:[db stringForQuery:@"select imageName from EFENGSHARE where shareId = ?",shareId]];
    }];
    return result;
}


- (EFengShare *)getShareWithShareIndex:(NSInteger)shareIndex
{
    __block EFengShare *share = nil;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARE where ShareIndex = ?", @(shareIndex)];
        while (result && [result next]) {
            share = [[EFengShare alloc] init];
            share.ShareIndex = [result intForColumn:@"ShareIndex"];
            share.shareId = [result stringForColumn:@"shareId"];
            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
            share.praiseCount = [result stringForColumn:@"praiseCount"];
            share.releaseType = [result stringForColumn:@"releaseType"];
            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
            share.videoName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"videoName"]];
            share.content = [result stringForColumn:@"content"];
            share.companyId = [result stringForColumn:@"companyId"];
            share.date = [result stringForColumn:@"TimeStamp"];
            share.isRead = [result boolForColumn:@"isRead"];
            share.isClick = [result boolForColumn:@"isClick"];
            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
            share.isZan = [result boolForColumn:@"isZan"];
            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
        }
    }];
    return share;

}

- (EFengShare *)getShareWithShareId:(NSString *)shareId
{
    __block EFengShare *share = nil;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARE where shareId = ?", shareId];
        while ([result next]) {
            share = [[EFengShare alloc] init];
            share.ShareIndex = [result intForColumn:@"ShareIndex"];
            share.shareId = [result stringForColumn:@"shareId"];
            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
            share.praiseCount = [result stringForColumn:@"praiseCount"];
            share.releaseType = [result stringForColumn:@"releaseType"];
            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
            share.videoName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"videoName"]];
            share.content = [result stringForColumn:@"content"];
            share.companyId = [result stringForColumn:@"companyId"];
            share.date = [result stringForColumn:@"TimeStamp"];
            share.isRead = [result boolForColumn:@"isRead"];
            share.isClick = [result boolForColumn:@"isClick"];
            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
            share.isZan = [result boolForColumn:@"isZan"];
            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
        }
    }];
    return share;

}
- (BOOL)delShareWithShareIndex:(NSInteger)shareIndex
{
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from EFENGSHARE where ShareIndex = ?",@(shareIndex)];
    }];
    return ret;

}
- (NSArray *)getUnSendShare
{
    __block NSMutableArray *arrayM = [NSMutableArray array];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARE where isLocal = 0 order AND shareId = ?", @""];
        while (result && [result next]) {
            EFengShare *share = [[EFengShare alloc] init];
            share.ShareIndex = [result intForColumn:@"ShareIndex"];
            share.shareId = [result stringForColumn:@"shareId"];
            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
            share.praiseCount = [result stringForColumn:@"praiseCount"];
            share.releaseType = [result stringForColumn:@"releaseType"];
            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
            share.videoName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"videoName"]];
            share.content = [result stringForColumn:@"content"];
            share.companyId = [result stringForColumn:@"companyId"];
            share.date = [result stringForColumn:@"TimeStamp"];
            share.isRead = [result boolForColumn:@"isRead"];
            share.isClick = [result boolForColumn:@"isClick"];
            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
            share.isZan = [result boolForColumn:@"isZan"];
            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [arrayM addObject:share];
            share = nil;
        }
    }];
    return arrayM;

}

- (BOOL)updateShareIsUnNetCompletedWithShare:(EFengShare *)share isDel:(BOOL)isDel
{
    if (share == nil) {
        return NO;
    }
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"UPDATE EFENGSHARE SET yuLiuZiduan2 = 1, isDel = ? where shareId = ?",isDel?@1:@0,share.shareId];
        
    }];
    return ret;

}

- (NSArray *)getShareListWithSenderUserId:(NSString *)sendUserId
{
    __block NSMutableArray *arrayM = [NSMutableArray array];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARE where isLocal = 0 AND isDel = 0 AND releaseUserId = ? order by ShareIndex  DESC LIMIT ?,?", sendUserId,@"0",@"20"];
        while (result && [result next]) {
            EFengShare *share = [[EFengShare alloc] init];
            share.ShareIndex = [result intForColumn:@"ShareIndex"];
            share.shareId = [result stringForColumn:@"shareId"];
            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
            share.praiseCount = [result stringForColumn:@"praiseCount"];
            share.releaseType = [result stringForColumn:@"releaseType"];
            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
            share.videoName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"videoName"]];
            share.content = [result stringForColumn:@"content"];
            share.companyId = [result stringForColumn:@"companyId"];
            share.date = [result stringForColumn:@"TimeStamp"];
            share.isRead = [result boolForColumn:@"isRead"];
            share.isClick = [result boolForColumn:@"isClick"];
            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
            share.isZan = [result boolForColumn:@"isZan"];
            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [arrayM addObject:share];
            share = nil;
        }
    }];
    return arrayM;

}

//- (NSArray *)getNewerShareWithMinShareIndex:(NSInteger)shareIndex releaseUserId:(NSString *)releaseUserId
//{
//    __block NSMutableArray *arrayM = [NSMutableArray array];
//    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
//        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARE where isLocal = 0 AND isDel = 0 AND releaseUserId = ? AND ShareIndex >= ? order by ShareIndex DESC LIMIT ?,?",releaseUserId,@(shareIndex),@"0",@"20"];
//        while ([result next]) {
//            EFengShare *share = [[EFengShare alloc] init];
//            share.ShareIndex = [result intForColumn:@"ShareIndex"];
//            share.shareId = [result stringForColumn:@"shareId"];
//            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
//            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
//            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
//            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
//            share.praiseCount = [result stringForColumn:@"praiseCount"];
//            share.releaseType = [result stringForColumn:@"releaseType"];
//            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
//            share.content = [result stringForColumn:@"content"];
//            share.companyId = [result stringForColumn:@"companyId"];
//            share.date = [result stringForColumn:@"TimeStamp"];
//            share.isRead = [result boolForColumn:@"isRead"];
//            share.isClick = [result boolForColumn:@"isClick"];
//            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
//            share.isZan = [result boolForColumn:@"isZan"];
//            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
//            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
//            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
//            [arrayM addObject:share];
//            share = nil;
//        }
//        [result close];
//    }];
//    return arrayM;
//
//}
//
//- (NSArray *)getOlderShareWithMaxShareIndex:(NSInteger)shareIndex releaseUserId:(NSString *)releaseUserId
//{
//    __block NSMutableArray *arrayM = [NSMutableArray array];
//    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
//        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARE where isLocal = 0 AND isDel = 0 AND releaseUserId = ? AND ShareIndex <= ? order by ShareIndex DESC LIMIT ?,?",releaseUserId,@(shareIndex),@"0",@"20"];
//        while ([result next]) {
//            EFengShare *share = [[EFengShare alloc] init];
//            share.ShareIndex = [result intForColumn:@"ShareIndex"];
//            share.shareId = [result stringForColumn:@"shareId"];
//            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
//            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
//            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
//            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
//            share.praiseCount = [result stringForColumn:@"praiseCount"];
//            share.releaseType = [result stringForColumn:@"releaseType"];
//            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
//            share.content = [result stringForColumn:@"content"];
//            share.companyId = [result stringForColumn:@"companyId"];
//            share.date = [result stringForColumn:@"TimeStamp"];
//            share.isRead = [result boolForColumn:@"isRead"];
//            share.isClick = [result boolForColumn:@"isClick"];
//            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
//            share.isZan = [result boolForColumn:@"isZan"];
//            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
//            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
//            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
//            [arrayM addObject:share];
//            share = nil;
//        }
//        [result close];
//    }];
//    return arrayM;
//
//}
- (NSArray *)getMineAttentionShareList
{
    __block NSMutableArray *arrayM = [NSMutableArray array];
    
//    NSLog(@"%@",attentionIds);
    __block NSString *sql = [NSString stringWithFormat:@"select * from EFENGSHARE where isLocal = 0 AND isDel = 0 AND releaseUserId in (select UserId from ATTENTIONCONTACT where isAttentionMe = 0) order by ShareIndex  DESC LIMIT \'%@\',\'%@\'",@"0",@"20"];
        [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while (result && [result next]) {
            EFengShare *share = [[EFengShare alloc] init];
            share.ShareIndex = [result intForColumn:@"ShareIndex"];
            share.shareId = [result stringForColumn:@"shareId"];
            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
            share.praiseCount = [result stringForColumn:@"praiseCount"];
            share.releaseType = [result stringForColumn:@"releaseType"];
            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
            share.videoName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"videoName"]];
            share.content = [result stringForColumn:@"content"];
            share.companyId = [result stringForColumn:@"companyId"];
            share.date = [result stringForColumn:@"TimeStamp"];
            share.isRead = [result boolForColumn:@"isRead"];
            share.isClick = [result boolForColumn:@"isClick"];
            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
            share.isZan = [result boolForColumn:@"isZan"];
            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [arrayM addObject:share];
            share = nil;
        }
    }];
    return arrayM;

}

- (EFengShare *)getMaxShareIndexShare
{
   
    __block EFengShare *share = nil;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@" select * from EFENGSHARE where isLocal = 0 AND ShareIndex = (select max(ShareIndex) from EFENGSHARE)"];
        while (result && [result next]) {
            share = [[EFengShare alloc] init];
            share.ShareIndex = [result intForColumn:@"ShareIndex"];
            share.shareId = [result stringForColumn:@"shareId"];
            share.releaseUserName = [result stringForColumn:@"releaseUserName"];
            share.releaseUserId = [result stringForColumn:@"releaseUserId"];
            share.visibleUserId = [result stringForColumn:@"visibleUserId"];
            share.praiseUserId = [result stringForColumn:@"praiseUserId"];
            share.praiseCount = [result stringForColumn:@"praiseCount"];
            share.releaseType = [result stringForColumn:@"releaseType"];
            share.imageName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"imageName"]];
            share.videoName = [NSArray arrayWihtJsonStr:[result stringForColumn:@"videoName"]];
            share.content = [result stringForColumn:@"content"];
            share.companyId = [result stringForColumn:@"companyId"];
            share.date = [result stringForColumn:@"TimeStamp"];
            share.isRead = [result boolForColumn:@"isRead"];
            share.isClick = [result boolForColumn:@"isClick"];
            share.isShowShanchu = [result boolForColumn:@"isShowShanchu"];
            share.isZan = [result boolForColumn:@"isZan"];
            share.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            share.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            share.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
        }
    }];
    return share;

}

- (BOOL)updateShareWithShareId:(NSString *)shareId date:(NSString *)date
{
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE EFENGSHARE SET TimeStamp = ?, yuLiuZiduan2 = 0 where shareId = ?",M_STOREVALUE(date),M_STOREVALUE(shareId)];
    }];
    return ret;

}
@end
